package com.xunji.xunji.module.account.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.huanxiao.base.base.BaseActivity;
import com.huanxiao.base.constant.Const;
import com.huanxiao.base.constant.SPConst;
import com.huanxiao.base.net.RespResult;
import com.huanxiao.base.util.AlertDialogUtil;
import com.huanxiao.base.view.ClearEditText;
import com.huanxiao.base.view.DesignToolbar;
import com.huanxiao.util.SPHelper;
import com.huanxiao.util.StringHelper;
import com.xunji.xunji.R;
import com.xunji.xunji.module.account.bean.TokenInfo;
import com.xunji.xunji.module.account.controller.UserAccount;

import java.util.Map;

import rx.Subscriber;

/**
 * 登录
 * Created by paul on 16/10/18.
 */

public class LoginActivity extends BaseActivity {

    private ClearEditText mCetPhone;
    private EditText mCetPassword;
    private Button mBtnLogin;
    private Button mBtnRegister;
    private DesignToolbar mToolbar;

    private TextWatcher mTextWatcher;


    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }


    @Override
    protected void onDestroy() {
        mCetPassword.removeTextChangedListener(mTextWatcher);
        mCetPhone.removeTextChangedListener(mTextWatcher);
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void initBundleExtras(Bundle extras) {

    }

    @Override
    protected void initView() {

        mBtnLogin = (Button) findViewById(R.id.btn_next_step);
        mBtnRegister = (Button) findViewById(R.id.btn_register);
        mCetPhone = (ClearEditText) findViewById(R.id.cet_phone);
        mCetPassword = fvById(R.id.cet_password);
        mToolbar = (DesignToolbar) findViewById(R.id.toolbar);

    }

    @Override
    protected void initListener() {

        mTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkValidate();
            }
        };

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        mCetPhone.addTextChangedListener(mTextWatcher);
        mCetPassword.addTextChangedListener(mTextWatcher);


        mToolbar.setOnRightMenuClickListener(new DesignToolbar.OnRightMenuClickListener() {
            @Override
            public void onRightClick() {
                ForgetPswdActivity.start(LoginActivity.this);
            }
        });


        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.start(LoginActivity.this);
            }
        });



    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mCetPhone.setText(SPHelper.get(SPConst.SP_LOGIN_PHONE, ""));
    }

    @Override
    protected boolean isBindEventBus() {
        return false;
    }


    private void checkValidate() {
        if (StringHelper.is11Number(mCetPhone.getText().toString())) {
            if (mCetPassword.getText().toString().length() >= 6) {
                mBtnLogin.setEnabled(true);
            } else {
                mBtnLogin.setEnabled(false);
            }
        } else {
            mBtnLogin.setEnabled(false);
        }
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.white;
    }


    private void login() {
        showLoading("正在登陆...");
        SPHelper.save(SPConst.SP_LOGIN_PHONE, mCetPhone.getText().toString().trim());
        UserAccount.getInstance()
                .login(mCetPhone.getText().toString(), mCetPassword.getText().toString(),
                        null,
                        new Subscriber<RespResult<String>>() {
                            @Override
                            public void onCompleted() {
                                hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                hideLoading();

                            }

                            @Override
                            public void onNext(RespResult<String> userInfoRespResult) {
                                hideLoading();
                                finish();

                            }
                        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


}
